package com.csxh.web.model;

import java.util.ArrayList;
import java.util.List;


public class BuyCart<T extends IBuyItem> {

	private List<T> itemList=new ArrayList<T>();
	
	public List<T> getItemList() {
		return itemList;
	}
	
	public void addItem(T item){
		
		for(T it : this.itemList){
			if(it.equals(item)){
				it.setQuantity(it.getQuantity()+item.getQuantity());
				return;
			}
		}
		item.setId(this.itemList.size());
		this.itemList.add(item);
	}
	
	public void deleteItem(int itemId){
		
		for(int i=this.itemList.size()-1;i>-1;i--){
			if(itemId==i){
				this.itemList.remove(i);
				return;
			}
		}
		
	}
	
	public void updateItem(int itemId,int quantity){
		
		for(int i=this.itemList.size()-1;i>-1;i--){
			if(itemId==i){
				this.itemList.get(i).setQuantity(quantity);
				return;
			}
		}
		
	}
	
	public IBuyItem findItem(int itemId){
		
		for(int i=this.itemList.size()-1;i>-1;i--){
			if(itemId==i){
				return this.itemList.get(i);
			}
		}
		
		return null;
	}
	
	public int getTotalQuantity(){
		int quantity=0;
		for(T item : this.itemList){
			quantity+=item.getQuantity();
		}
		return quantity;
	}

	public int getTotalWeight(){
		int weight=0;
		for(T item : this.itemList){
			weight+=item.getTotalWeight();
		}
		return weight;
	}
	
	public double getTotalPrice(){
		double price=0.0d;
		for(T item : this.itemList){
			price+=item.getTotalPrice();
		}
		return price;
	}
	
	
}
